<?php

namespace Modules\Admin\Http\Controllers;

use App\Models\Activity;
use App\Models\Admin;
use App\Models\AdminMenu;
use App\Models\AdminRole;
use App\Models\Banner;
use App\Models\Goods;
use App\Models\GoodsClass;
use App\Models\Order;
use App\Models\Recharge;
use App\Models\System;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Routing\Controller;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Session;

class AdminController extends Controller
{
    public function index()
    {
        return view('admin::login/login');
    }

    public function console(){
        return view('admin::index/console');
    }

    public function home()
    {
        $adminId = Session::get("adminId");
        $admin = Admin::where("id",$adminId)->with("adminRole")->first();
        $menu = json_decode($admin->adminRole->resource,true);
        return view('admin::index/index',["menu"=>$menu,"admin"=>$admin->adminRole->name]);
    }

    public function banner()
    {
        return view('admin::banner/list');
    }

    public function bannerAdd()
    {
        return view('admin::banner/add');
    }

    public function bannerEdit(Request $request){
        $id = $request->input("id");
        $banner = Banner::find($id);
        return view('admin::banner/edit',["data"=>$banner]);
    }

    public function classList()
    {
        return view('admin::class/list');
    }

    public function classAdd(){
        return view('admin::class/add');
    }

    public function classEdit(Request $request){
        $id = $request->input("id");
        $goodsclass = GoodsClass::find($id);
        return view('admin::class/edit',["data"=>$goodsclass]);
    }

    public function goodsList(){
        return view('admin::goods/list');
    }

    public function goodsAdd(){
        return view('admin::goods/add');
    }

    public function goodsEdit(Request $request){
        $id = $request->input("id");
        $goods = Goods::where("id",$id)->with("sku")->with("color")->first();
        return view("admin::goods/edit",["goods"=>$goods]);
    }

    public function colorAdd(){
        return view('admin::goods/color_add');
    }

    public function menuList(){
        return view('admin::user/menu');
    }

    public function menuAdd(){
        return view('admin::user/menu_add');
    }

    public function roleList(){
        return view('admin::user/role');
    }

    public function roleAdd(){
        //查询一级菜单
        $oneMenu = AdminMenu::where("prev_id",0)->get();
        if ($oneMenu->count()<1)return self::error(1003);
        $menuArray = [];
        foreach ($oneMenu as $item){
            //查询二级菜单
            $secondMenu = AdminMenu::where("prev_id",$item->id)->get();
            $item['child'] = $secondMenu;
            $menuArray[] = $item;
        }
        return view('admin::user/role_add',["menu"=>$menuArray]);
    }

    public function roleEdit(Request $request){
        $id = $request->input("id");
        $role = AdminRole::find($id);
        $resourece = json_decode($role->resource);
        //查询一级菜单
        $oneMenu = AdminMenu::where("prev_id",0)->get();
        if ($oneMenu->count()<1)return self::error(1003);
        $menuArray = [];
        foreach ($oneMenu as $item){
            $bool = AdminMenu::searchMenu($item->id,$resourece);
            $item['checked'] = $bool;

            //查询二级菜单
            $secondMenu = AdminMenu::where("prev_id", $item->id)->get();
            foreach ($secondMenu as $value){
                $bool = AdminMenu::searchMenu($value->id,$resourece);
                $value['checked'] = $bool;
            }
            $item['child'] = $secondMenu;
            $menuArray[] = $item;
        }
        return view("admin::user/role_edit",["menu"=>$menuArray,"id"=>$id]);
    }

    public function userList(){
        return view("admin::user/user");
    }

    public function userAdd(){
        return view("admin::user/user_add");
    }

    public function orderList(){
        return view("admin::order/list");
    }

    public function orderSend(Request $request){
        $id = $request->input("id");
        $order = Order::find($id);
        return view("admin::order/send",["order"=>$order]);
    }

    public function orderDetail(Request $request){
        $id = $request->input("id");
        $order = Order::where("id",$id)->with('user')->with('orderGoods')->first();
        return view("admin::order/detail",["order"=>$order]);
    }

    public function activityList(){
        return view("admin::activity/list");
    }

    public function activityAdd(){
        return view("admin::activity/add");
    }

    public function activityEdit(Request $request){
        $id = $request->input("id");
        $activity = Activity::find($id);
        return view("admin::activity/edit",["data"=>$activity]);
    }

    public function scoreList(){
        return view("admin::score/list");
    }

    public function memberList(){
        return view("admin::member/list");
    }

    public function systemHtml(){
        $system = System::find(1);
        return view("admin::system/system",["sys"=>$system]);
    }

    public function goodsDataHtml(Request $request){
        $id = $request->input("id");
        return view("admin::goods/data",["id"=>$id]);
    }

    public function dataGoodsHtml(){
        return view("admin::data/goods");
    }

    public function dataOrderHtml(){
        return view("admin::data/order");
    }

    public function memberAddHtml(){
        return view("admin::member/add");
    }

    public function goodsPingHtml(Request $request){
        $id = $request->input("id");
        return view("admin::ping/list",["id"=>$id]);
    }

    public function goodsPingAddHtml(Request $request){
        $id = $request->input("id");
        return view("admin::ping/add",["id"=>$id]);
    }

    public function scoreRecordsHtml(){
        return view("admin::score/records");
    }

    public function orderShowHtml(){
        return view("admin::order/show");
    }

    public function rechargeHtml(){
        return view("admin::score/recharge");
    }

    public function rechargeAddHtml(){
        return view("admin::score/recharge_add");
    }

    public function rechargeEditHtml(Request $request){
        $id = $request->input("id");
        $recharge = Recharge::find($id);
        return view("admin::score/recharge_edit",["data"=>$recharge]);
    }

    public function payHtml(){
        return view("admin::score/pay");
    }
}
